﻿using Game;
using Mekiasm.Interface;
using Mekiasm.Machines;

namespace Mekiasm.Machine
{
    public class ComponentMetallurgicInfuser : ComponentEnergyMachine
    {
        public override IDynamicCraft CraftDevice => MekCraftingRecipe.FindCraftDevice(typeof(MetallurgicInfuser));
        public override int CraftPowerUse => 32;
        public override bool CheckResultCapacity => true;
        public void IncreaseFuel()
        {
            var fuelSlot = Slots[0];
            int slotValue = fuelSlot.Value;
            var block = BlocksManager.Blocks[Terrain.ExtractContents(slotValue)];
            if (block is MekBlock mek)
            {
                var item = mek.GetItem(ref slotValue);
                if (item is Interface.IInfuserableItem item2)
                {
                    if (fuelSlot.Count > 0 && (fuelSlot.FuelAmount == 0 || (fuelSlot.FuelValue == item2.InfuserValue && fuelSlot.FuelAmount + item2.InfuserAmount <= fuelSlot.FuelCapacity)))
                    {
                        fuelSlot.Count--;
                        fuelSlot.FuelValue = item2.InfuserValue;
                        fuelSlot.FuelType = FuelType.Dust;
                        fuelSlot.FuelAmount += item2.InfuserAmount;
                    }
                }
            }
        }

        public override bool CanAcceptFuel(int slotIndex, int fuelValue)
        {
            var block = BlocksManager.Blocks[Terrain.ExtractContents(fuelValue)];
            if (block is MekBlock mek)
            {
                var item = mek.GetItem(ref fuelValue);
                if (item is Interface.IInfuserableItem)
                {
                    return true;
                }
            }
            return base.CanAcceptFuel(slotIndex, fuelValue);
        }

        public override int GetSlotCapacity(int slotIndex, int value)
        {
            var block = BlocksManager.Blocks[Terrain.ExtractContents(value)];
            if (slotIndex == 0)
            {
                if (block is MekBlock mek)
                {
                    var item = mek.GetItem(ref value);
                    if (item is Interface.IInfuserableItem)
                    {
                        return mek.GetMaxStacking(value);
                    }
                }
                return 0;
            }else  return block.GetMaxStacking(value);
        }
    }
}
